//
//  LHBrowseController.m
//  WXYView
//
//  Created by lehuo on 2017/1/16.
//  Copyright © 2017年 lehuo. All rights reserved.
//

#import "LHBrowseController.h"
#import "LHBrowseCollectionCell.h"
#import <Photos/Photos.h>
#import "GoalData.h"
#import "PhotoAssetModel.h"
#import "PhotoGroupModel.h"

@interface LHBrowseController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UIButton *sendBtn;//发送按钮
@property(nonatomic,strong)UIButton *selectBtn;//选中按钮
@property(nonatomic,strong)UILabel *indicateLabel;
@property(nonatomic,strong)UIButton *originBtn;//原图按钮
@property(nonatomic,strong)UIView *headV;//头部工具栏
@property(nonatomic,strong)UIView *tailV;//尾部工具栏
@property(nonatomic,strong)NSMutableArray <PhotoAssetModel *>*selectAssets;
@property(nonatomic,assign)NSUInteger index;//标示当前cell的位置

@end

@implementation LHBrowseController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self initView];
    [self setHeadTool];
    [self setTailTool];
    
    [self.collectionView scrollToItemAtIndexPath:_indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    _index = self.collectionView.contentOffset.x / kSceenW;
    self.selectBtn.selected = self.assets[_index].isSelect;
    [self setSelectBtnTitle];
}

- (void)setSelectBtnTitle
{
    NSUInteger count = 0;
    for (PhotoGroupModel *groupModel in self.photoModels)
    {
        for (PhotoAssetModel*assetModel in groupModel.assets) {
            if (assetModel.isSelect) {
                count ++;
            }
        }
    }
    
    if (count > 0) {
        self.sendBtn.selected = YES;
        [self.sendBtn setTitle:[NSString stringWithFormat:@"确认(%zd)",count] forState:UIControlStateSelected];
        self.sendBtn.enabled = YES;
    }else
    {
        self.sendBtn.selected = NO;
        [self.sendBtn setTitle:[NSString stringWithFormat:@"确认(0)"] forState:UIControlStateNormal];
        self.sendBtn.enabled = NO;
    }
}

- (void)initView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(kSceenW,kSceenH);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kSceenW, kSceenH) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[LHBrowseCollectionCell class] forCellWithReuseIdentifier:@"CCell"];
    [self.view addSubview:_collectionView];
    
    _indicateLabel = [[UILabel alloc] init];
    _indicateLabel.textColor = [UIColor whiteColor];
    _indicateLabel.font = [UIFont boldSystemFontOfSize:15];
    _indicateLabel.textAlignment = NSTextAlignmentCenter;
    _indicateLabel.frame = CGRectMake(0, kSceenH - 50, kSceenW, 30);
    _indicateLabel.text = [NSString stringWithFormat:@"%zd/%zd",self.indexPath.row,self.assets.count];
    [self.view addSubview:_indicateLabel];
    
}

#pragma mark - 添加头部底部工具栏
- (void)setHeadTool
{
    float space = 20;
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(space,space, 19, 26)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_imagePicker"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(kSceenW - space - 30, space, 30, 30)];
    self.selectBtn = selectBtn;
    [selectBtn setBackgroundImage:[UIImage imageNamed:@"checkbox_pic_non"] forState:UIControlStateNormal];
    [selectBtn setBackgroundImage:[UIImage imageNamed:@"checkbox_pic"] forState:UIControlStateSelected];
    [selectBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSceenW, 60)];
    [headV addSubview:backBtn];
    [headV addSubview:selectBtn];
    headV.backgroundColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:0.8];;
    self.headV = headV;
    [self.view addSubview:headV];
    
}


- (void)setTailTool
{
    float space = 20;
    float btnH = 20;
    UIButton *originBtn = [[UIButton alloc] initWithFrame:CGRectMake(space, space * 0.5, 140, btnH)];
    self.originBtn = originBtn;
    [originBtn setTitle:@"原图" forState:UIControlStateNormal];
    originBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [originBtn addTarget:self action:@selector(originClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(kSceenW - space - 80, space * 0.5, 100, btnH)];
    self.sendBtn = sendBtn;
    [sendBtn setTitle:@"选中(0)" forState:UIControlStateNormal];
    [sendBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [sendBtn setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
    [sendBtn addTarget:self action:@selector(sendClik) forControlEvents:UIControlEventTouchUpInside];
    static float tailH = 44;
    UIView *tailV = [[UIView alloc] initWithFrame:CGRectMake(0, kSceenH - tailH, kSceenW,tailH)];
    [tailV addSubview:originBtn];
    [tailV addSubview:sendBtn];
    self.tailV = tailV;
    tailV.backgroundColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:0.8];
    [self.view addSubview:tailV];
}

#pragma mark - 点击时间
//返回
- (void)backClick
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadView" object:nil];
}
//选择
- (void)selectClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    self.sendBtn.selected = YES;
    self.assets[_index].isSelect = btn.selected;
    [self setSelectBtnTitle];
}
//查看原图
- (void)originClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    ALAsset *asset = self.assets[_index].asset;
    
    
    if (btn.selected)
    {
        [btn setTitle:[NSString stringWithFormat:@"原图(%@)",[self sizeOfAsset:asset]] forState:UIControlStateSelected];
    }else
    {
        [btn setTitle:@"原图" forState:UIControlStateNormal];
    }
    
    
    
    
}

//确认选中
- (void)sendClik
{
    NSLog(@"send");
    for (PhotoGroupModel *groupModel in self.photoModels)
    {
        for (PhotoAssetModel*assetModel in groupModel.assets) {
            if (assetModel.isSelect) {
                [self.selectAssets addObject:assetModel];
            }
        }
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:PostData object:@{@"Data":self.selectAssets}];
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - UICollectionView代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"CCell";
    LHBrowseCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    [cell setCellValue:self.assets[indexPath.row]];
    //单击图片回调
    cell.didTap = ^{
        self.headV.hidden = !self.headV.hidden;
        self.tailV.hidden = !self.tailV.hidden;
    };
    self.indexPath = indexPath;
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    scrollView.showsHorizontalScrollIndicator = NO;
    NSUInteger index =  scrollView.contentOffset.x / kSceenW;
    _indicateLabel.text = [NSString stringWithFormat:@"%zd/%zd",index + 1,self.assets.count];
    _index = index;
    ALAsset *asset = [self.assets objectAtIndex:index].asset;
    self.selectBtn.selected = [self.assets objectAtIndex:index].isSelect;
    if (self.originBtn.selected)
    {
        [self.originBtn setTitle:[NSString stringWithFormat:@"原图(%@)",[self sizeOfAsset:asset]] forState:UIControlStateSelected];
    }else{
        [self.originBtn setTitle:@"原图" forState:UIControlStateNormal];
    }
}

#pragma mark - 计算原图尺寸
- (NSString *)sizeOfAsset:(ALAsset *)asset
{
    NSUInteger unitMB = 1024 * 1024;
    NSUInteger unitKB = 1024;
    long long size = [[asset defaultRepresentation] size];
    
    float unitSize = size / unitMB;
    NSString * str;
    if (unitSize < 1) {
        str = [NSString stringWithFormat:@"%.2fKB",(float)size / unitKB];
    }else
    {
        str = [NSString stringWithFormat:@"%.2fMB",unitSize];
    }
    return str;
}

#pragma mark - lazy loading
- (NSMutableArray<PhotoAssetModel *> *)selectAssets
{
    if (!_selectAssets)
    {
        _selectAssets = [NSMutableArray array];
    }
    return _selectAssets;
}

@end
